<template>
  <!-- 新增部门的弹层 -->
  <!-- el-dialog 内部 $emit('update:visible', false) -->
  <!-- :visible 不能加 sync，加了 sync 点击 x 或阴影的时候 el-dialog 组件内部会修改 showDialog -->
  <!-- 而 showDialog 是外部传递过来的简单数据类型，不能改 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
    <!-- 表单组件 el-form，通过 label-width 设置 label 的宽度  -->
    <!-- 匿名插槽 -->
    <el-form ref="formRef" v-loading="loading" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="3-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          v-model="formData.managerId"
          style="width: 80%"
          placeholder="请选择"
        >
          <!-- label 就是展示的文案，给人看的 -->
          <!-- value 就是收集的数据，传到后端的 -->
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog 有专门放置底部操作栏的插槽，具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为 24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addDept">确定</el-button>
        <el-button size="small">取消--{{ currentNodeId }}</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { checkRepeat } from '@/utils/validate'
import { addDepartments, getManagerList, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    const _this = this
    return {
      loading: false,
      formData: {
        name: '', // 部门名称，必填 1-50 个字符，同级部门中禁止出现重复部门
        code: '', // 部门编码，必填 1-50 个字符，部门编码在整个模块中都不允许重复
        managerId: '', // 部门管理者，必填
        introduce: '', // 部门介绍，必填 1-300 个字符
        pid: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名字',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 50,
            message: '请输入 3~50 个字符',
            trigger: 'blur'
          },
          {
            validator: checkRepeat('name', _this),
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: checkRepeat('code', _this),
            trigger: 'blur'
          }
        ],
        managerId: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: 'change'
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            min: 1,
            max: 300,
            message: '部门介绍要求1-50个字符'
          }
        ]
      },
      managerList: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '添加部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    closeDialog() {
      // 情况数据的内容和校验规则
      // !resetFields => 它只能置空和 form 表单绑定的字段
      this.formData = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.formRef.resetFields()
      // 通知父亲更新 showDialog
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      this.managerList = await getManagerList()
    },
    async addDept() {
      try {
        await this.$refs.formRef.validate()
        // #1 根据有无 id 调用添加或编辑接口
        if (this.formData.id) {
          // !一定不要少了 await
          await updateDepartments(this.formData)
        } else {
          await addDepartments({
            ...this.formData,
            pid: this.currentNodeId
          })
        }
        // #2 重新渲染数据（通知父亲重新调用获取数据并更新的方法）
        this.$emit('updateList')
        // #3 成功提示
        this.$message.success(this.formData.id ? '编辑成功' : '添加成功')
        // #4 关闭弹框
        this.closeDialog()
      } catch (e) {
        console.log(e)
      }
    },
    async getDepartDetail() {
      this.loading = true
      this.formData = await getDepartDetail(this.currentNodeId)
      this.loading = false
    }
  }

}
</script>
